package com.lsc.service;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

@Service
public class MailSendServiceImpl implements MailSendService {
    
	@Autowired
    private JavaMailSender javaMailSender;
	
	@Value("${spring.mail.username}")
	private String from;
	
	@Value("${spring.mail.to}")
	private String to;
	
	public void sendMail(String subject,String text)
	{
		SimpleMailMessage message=new SimpleMailMessage();
		message.setFrom(from);
		message.setTo(to);
		message.setSubject(subject);
		message.setText(text);
		javaMailSender.send(message);
	}
	
	/**
     * 发送复杂邮件 html内容或带附件
     */
    private void sendMailAddFile() {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setSubject("这是测试邮件");
            // 发送html格式内容
            helper.setText("<font style='color:red'>这是正文</font>",true);
            helper.setTo(to);
            helper.setFrom(from);
 
            //添加附件
            helper.addAttachment("1.jpg",new File("c:\\1.jpg"));
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        javaMailSender.send(message);
    }
}
